/**
 * @Description: gitCommit指令
 * @author Beon
 * @date 2021/1/14
*/
import * as dedent from 'dedent';
import * as childProcess from 'child_process';

export class Commit{

    /**
     * 执行commit
     * @param {String} repoPath 执行路径
     * @param {String} message 提交信息
     * @returns {Promise<void>}
     */
    static async dealCommit(message: string[], repoPath?: string):Promise<any> {
        try {
            let args = ['commit', '-m', '"' + dedent(message[0]) + '"', '-m', '"' + dedent(message[1]) + '"'];
            return this.resultAn(await childProcess.execSync(`git ${args.join(' ')}`, {
                cwd: repoPath
            }));
        } catch (e) {
            console.log(e);
        }
    }

    /**
     * 执行提交后续操作
     * @param child
     */
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    static resultAn(child: any): any {
        if (!child.stderr) {
            if (child.status){
                if (child.status === 128) {
                    console.warn(`
            Git exited with code 128. Did you forget to run:

              git config --global user.email "you@example.com"
              git config --global user.name "Your Name"
            `);
                }
                Error(`git exited with error code ${child.status}`);
            }
        }
        return child;
    }
}
